package field

import (
	"strings"
	"yunj/pkg/yunj/core/builder"
	"yunj/pkg/yunj/core/builder/builderdto"
	"yunj/pkg/yunj/core/builder/form"
)

// 树-复选
type TreeCheckbox struct {
	form.Field
	Nodes                []*builderdto.FormTreeCheckNode `json:"nodes"`       // 节点（示例:[]*dto.FormTreeCheckNode{{Id:1,Name:"xiaowang",Pid:0,Nocheck:false,Checked:true},{Id:2,Name:"xiaoli",Pid:1}}）
	CloseLevel           uint                            `json:"closeLevel"`  // 收起等级（默认0不收起）
	LastChecked          bool                            `json:"lastChecked"` // 是否末级可选，反之所有可选
	Correlation          []bool                          `json:"correlation"` // 关联关系，[]bool{勾选时关联父,勾选时关联子,取消勾选时关联父,取消勾选时关联子}。默认关联所有
	activeNodeIdCommaStr string                          // 可选择的节点id逗号分割字符串
}

func NewTreeCheckbox(id string, title ...string) *TreeCheckbox {
	f := &TreeCheckbox{}
	form.SetFieldInstance(&f.Field, f)
	f.SetFieldId(id)
	if len(title) > 0 {
		f.SetTitle(title[0])
	}
	return f
}

// 设置标题
func (f *TreeCheckbox) SetTitle(title string) *TreeCheckbox {
	f.Title = title
	return f
}

// 设置默认值
func (f *TreeCheckbox) SetDefault(def any) *TreeCheckbox {
	f.Default = def
	return f
}

// 设置描述
func (f *TreeCheckbox) SetDesc(desc string) *TreeCheckbox {
	f.Desc = desc
	return f
}

// 设置验证规则
func (f *TreeCheckbox) SetVerify(rule string) *TreeCheckbox {
	f.Verify = rule
	return f
}

// 设置验证标题
func (f *TreeCheckbox) SetVerifyTitle(verifyTitle string) *TreeCheckbox {
	f.VerifyTitle = verifyTitle
	return f
}

// 增加验证规则
func (f *TreeCheckbox) AddVerify(rule string) *TreeCheckbox {
	f.AddFieldVerify(rule)
	return f
}

// 设置是否只读
func (f *TreeCheckbox) SetReadonly(yes bool) *TreeCheckbox {
	f.Readonly = yes
	return f
}

// 设置栅格布局
// 超小屏幕(手机<768px)、小屏幕(平板≥768px)、中等屏幕(桌面≥992px)、大型屏幕(桌面≥1200px)、超大屏幕(桌面≥1400px)
// 总列数：12
func (f *TreeCheckbox) SetGrid(num ...any) *TreeCheckbox {
	f.Grid = num
	return f
}

// 设置权限
func (f *TreeCheckbox) SetAuth(auth string) *TreeCheckbox {
	f.Auth = auth
	return f
}

// 设置节点
// 示例:[]*dto.FormTreeCheckNode{{Id:1,Name:"xiaowang",Pid:0,Nocheck:false,Checked:true},{Id:2,Name:"xiaoli",Pid:1}}
func (f *TreeCheckbox) SetNodes(nodes []*builderdto.FormTreeCheckNode) *TreeCheckbox {
	f.Nodes = nodes
	return f
}

// 收起等级（默认0不收起）
func (f *TreeCheckbox) SetCloseLevel(level uint) *TreeCheckbox {
	f.CloseLevel = level
	return f
}

// 是否末级可选，反之所有可选
func (f *TreeCheckbox) SetLastChecked(yes bool) *TreeCheckbox {
	f.LastChecked = yes
	return f
}

// 是否末级可选，反之所有可选
// 参数：勾选时关联父,勾选时关联子,取消勾选时关联父,取消勾选时关联子
func (f *TreeCheckbox) SetCorrelation(checkedParent, checkedChild, uncheckedParent, uncheckedChild bool) *TreeCheckbox {
	f.Correlation = []bool{checkedParent, checkedChild, uncheckedParent, uncheckedChild}
	return f
}

// 处理属性
func (f *TreeCheckbox) HandleAttr() error {
	// 处理节点
	err := f.HandleNodes()
	if err != nil {
		return f.SetError(err)
	}

	// Verify
	if !strings.Contains(f.Verify, "arrayIn") {
		if f.Verify == "" {
			f.Verify = "arrayIn:" + f.activeNodeIdCommaStr
		} else {
			f.Verify += "|arrayIn:" + f.activeNodeIdCommaStr
		}
	}

	// correlation 关联关系
	if f.Correlation == nil {
		f.Correlation = []bool{}
	}
	if len(f.Correlation) < 4 {
		i := len(f.Correlation)
		for i < 4 {
			f.Correlation = append(f.Correlation, true)
			i++
		}
	}

	return nil
}

// 处理节点
func (f *TreeCheckbox) HandleNodes() (err error) {
	activeNodeIdCommaStr, err := builder.HandleTreeCheckNodes(f.Nodes, f.LastChecked)
	if err != nil {
		return
	}
	f.activeNodeIdCommaStr = activeNodeIdCommaStr
	return
}
